//
//  MyProductViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/14.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "MyProductViewController.h"
#import "MyProductCell.h"
#import "ProductOrderViewController.h"
#import "ProductDetailsViewController.h"

@interface MyProductViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *meDevelop;
@property (weak, nonatomic) IBOutlet UIButton *meBuy;
@property (weak, nonatomic) IBOutlet UIImageView *dImage;
@property (weak, nonatomic) IBOutlet UIImageView *bImage;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *myArray;
@property (nonatomic, strong) NSArray *buyArray;

@end

@implementation MyProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"我的产品"];
    self.bImage.hidden = YES;
    [self requestData:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestData:(BOOL)ifBuy {
    NSString *string = ifBuy?@"4":@"3";
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"type":string, @"productType":@"",@"productStyle":@"",@"designerId":@""};
    [RequestData requestProductParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            if (ifBuy) {
                self.buyArray = jsonArray;
            } else {
                self.myArray = jsonArray;
            }
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark - action methods
- (IBAction)developButton:(id)sender {
    self.bImage.hidden = YES;
    self.dImage.hidden = NO;
    [self.myTableView reloadData];
}

- (IBAction)buyButton:(id)sender {
    self.bImage.hidden = NO;
    self.dImage.hidden = YES;
    if (!self.buyArray) {
        [self requestData:YES];
    } else {
        [self.myTableView reloadData];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.bImage.hidden?self.myArray.count:self.buyArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *myProductCellIdentifier = @"MyProductCell";
    [self.myTableView registerNib:[UINib nibWithNibName:@"MyProductCell" bundle:nil]
           forCellReuseIdentifier:myProductCellIdentifier];
    MyProductCell *cell = [tableView dequeueReusableCellWithIdentifier:myProductCellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MyProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myProductCellIdentifier];
    }
    cell.model = [self.bImage.hidden?self.myArray:self.buyArray objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.myTableView deselectRowAtIndexPath:indexPath animated:NO];
//    if (!self.ifOrder) {
        ProductDetailsViewController *view = [[ProductDetailsViewController alloc] init];
        [self.navigationController pushViewController:view animated:YES];
        view.productId = ((ProductModel *)[self.bImage.hidden?self.myArray:self.buyArray objectAtIndex:indexPath.row]).productId;
        [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
//    } else {
//        ProductOrderViewController *view =[[ProductOrderViewController alloc] init];
//        [self.navigationController pushViewController:view animated:YES];
//    }
}

@end
